package com.woniu.controller;

import com.woniu.pojo.*;
import com.woniu.service.SchoolService;
import com.woniu.service.UserService;
import com.woniu.service.WorkService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("EduManage")//教务管理
public class WorkController {
    @Resource
    private WorkService workService;
    @Resource
    private UserService userService;
    @Resource
    private SchoolService schoolService;
    /**
     * 加班列表展示页面
     *          返回schoolList 查询出所有school信息
     *          返回userList 查询出所有user信息
     * @return  worklist
     */
    @RequestMapping("worklist")
    public String worklist(Model model) {
        //school列表
        List<School> schoolList=new ArrayList<>();
        schoolList = schoolService.findAllSch();
        model.addAttribute("schoolList",schoolList);
        //user列表
        List<User> userList=new ArrayList<>();
        userList=userService.findAll4workselect();
        model.addAttribute("userList", userList);
        return "EduManage/worklist";
    }

    /**
     *  校区查询校区表
     * @return
     */
    public List<School> schools(){

        return null;
    }



    /**
     *  加班汇总
     *  分页查询
     *
     * @param pageBean
     * @param work
     * @return    worklist
     */

    @RequestMapping("worklistDo")
    @ResponseBody
    public Map<String, Object> worklistDo(PageBean pageBean, Work work) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Work> workList=workService.getWorkList(pageBean, work);
            resultMap.put("workList", workList);
            resultMap.put("pageBean", pageBean);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 加班审核列表页面
     *
     * @return
     */
    @RequestMapping("workaudit")
    public String workaudit() {
        return "EduManage/workaudit";
    }

    /**
     * 加班审核
     * 按照审核和未审核查询
     * @param pageBean
     * @param work
     * @return
     */
    @RequestMapping("workauditDo")
    @ResponseBody
    public Map<String, Object> workauditDo(PageBean pageBean, Work work) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Work> workList=workService.getWorkAudit(pageBean, work);
            resultMap.put("workList", workList);
            resultMap.put("pageBean", pageBean);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     * 加班申请
     *
     * @return
     */
    @RequestMapping("workapply")
    public String workapply(HttpSession session,Model model) {
        List<School> schoolList=new ArrayList<>();
        schoolList = schoolService.findAllSch();
        model.addAttribute("schoolList",schoolList);
        User user= (User) session.getAttribute("loginUser");
        model.addAttribute("user",user);
        List<Work> ownWorkList=workService.getWorkByUid(user.getUid());
        model.addAttribute("ownWorkList",ownWorkList);
        return "EduManage/workapply";
    }
    //查看当前登录用户的加班申请信息
    @RequestMapping("worklistByUid")
    @ResponseBody
    public Map<String, Object> worklistDo(PageBean pageBean, Work work,HttpSession session) {
        Map<String, Object> resultMap = new HashMap<>();
      User user= (User) session.getAttribute("loginUser");
        try {
            List<Work> workList=workService.getUserWorkList(pageBean, work,user);
              resultMap.put("workList", workList);
            resultMap.put("pageBean", pageBean);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
        }
        return resultMap;
    }
    /*
    根据ID删除加班信息
     */
    @RequestMapping("deleteWorkDo")
    @ResponseBody
    public Map<String, Object> deleteWorkDo(Integer wno){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            workService.deleteWork(wno);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //修改填错的加班申请信息，得到要修改的信息
    @RequestMapping("toUpdateWorkDo")
    @ResponseBody
    public Map<String, Object> toUpdateWorkDo(Integer wno){
        Map<String, Object> resultMap = new HashMap<>();
        try {
         Work work=   workService.findOneWorkByWno(wno);
            resultMap.put("work", work);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result",false);
        }
        return resultMap;
    }
    @RequestMapping("updateWorkDo")
    @ResponseBody
    public Map<String,Object> updateWorkDo(Work work){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            workService.updateWorkByWno(work);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * 加班审批
     *    修改审核状态(讲未审核改为已审核)
     *      需要判断判断审核状态是否为"未审核"，如果为"未审核"将会出现审批按钮进行审批操作
     * @return
     */
    @RequestMapping("toUpdateWstateDo")
    @ResponseBody
    public Map<String, Object> toUpdateWstate(Integer wno){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            workService.updateWstate(wno);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result",false);
        }
        return resultMap;
    }

    /**
     * 加班申请
     *      （新增）
     * @param
     * @return
     */
    @RequestMapping("workSaveDo")
    @ResponseBody
    public Map<String,Object> workSaveDo(Work work){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            workService.save(work);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * 前端传输的date数据
     * @param binder
     * @param request
     */
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }


}
